package com.jiantong.web.open.app.work;


import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.DisinfectReport;
import com.jiantong.work.service.DisinfectReportReadService;
import com.jiantong.work.service.DisinfectReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 *@author 毛文杰
 * -- 防疫中心-消毒记录
 */
@Slf4j
@OpenBean
public class DisinfectReportApis {
    @RpcConsumer
    private DisinfectReportReadService disinfectReportReadService;
    @RpcConsumer
    private DisinfectReportWriteService disinfectReportWriteService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @OpenMethod(
            key = "find.disinfect.report.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public DisinfectReport findById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<DisinfectReport> resp = disinfectReportReadService.findById(id);
        if (!resp.isSuccess()){
            log.error("failed to disinfect report by id = {}, cause : {}", id, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

    @OpenMethod(
            key = "create.disinfect.report",
            paramNames = {"disinfectReport","toStatus"},
            httpMethods = RequestMethod.POST
    )
    public Map<String,Object> create(DisinfectReport disinfectReport,Integer toStatus){
        Response<Map<String,Object>>response = disinfectReportWriteService.create(disinfectReport,toStatus);
        if(!response.isSuccess()){
            log.error("failed to create disinfect report disinfectReport = {}, cause : {}", disinfectReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "paging.disinfect.report",
            paramNames = {"pageNo", "pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<DisinfectReport> pagingDisinfectReport(Integer pageNo, Integer pageSize,
                                                         Integer status, Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Response<Paging<DisinfectReport>> resp =
                disinfectReportReadService.pagingDisinfectReport(pageNo, pageSize, status, breedFactoryId);
        if (!resp.isSuccess()) {
            log.error("failed to paging disinfect report{}, cause : {}", resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

    @OpenMethod(key = "update.disinfect.report",
            paramNames = {"disinfectReport","toStatus"},
            httpMethods=RequestMethod.PUT
    )
    public Map<String,Object> update(DisinfectReport disinfectReport,Integer toStatus){
        if(disinfectReport.getId() == null){
            log.error("id should be not null error = {}, cause : {}", disinfectReport,disinfectReport.getId());
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Map<String , Object>> statusResponse = disinfectReportReadService.findStatusById(disinfectReport.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(disinfectReport.getStatus())){
            log.error("disinfectReport is already submitted error = {}, cause : {}", disinfectReport.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),
                    "this.report.is.already.submitted");
        }

        Response<Map<String,Object>>response = disinfectReportWriteService.update(disinfectReport,toStatus);
        if (!response.isSuccess()){
            log.error("failed to update  disinfect report by diagnosisReport = {}, cause : {}", disinfectReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }

    /**
     * 政府端时间查询
     *
     * @return status =1
     */
    @OpenMethod(
            key = "paging.status.disinfect.report",
            paramNames = {"pageNo", "pageSize", "breedFactoryId","breedType", "fromDate", "toDate"},
            httpMethods = RequestMethod.GET)
    public Paging<DisinfectReport> pagingDisinfectReport(Integer pageNo,
                                                         Integer pageSize,
                                                         Long breedFactoryId,
                                                         Integer breedType,
                                                         String fromDate,
                                                         String toDate) {
        Date start = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            start = DateTime.parse(fromDate).toDate();
        }

        Date end = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<DisinfectReport>> resp =
                disinfectReportReadService.pagingDisinfectReportGqv(pageNo, pageSize, breedFactoryId,breedType, start, end);
        if (!resp.isSuccess()) {
            log.error("failed to paging disinfectReport center{}, cause : {}", resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

}
